package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.common.ResultTypeVo;
import com.zp.domain.Type;
import com.zp.util.Constant;

/**
 * test Type services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/9
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunTypeServices {

	private static final Log logger = LogFactory
			.getLog(RunTypeServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest/";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	@Test
	public void testAddType() {
		try {
			Type type = new Type();
			type.setName("四川-成都");
			type.setCode("028");
		    type.setParentCode("0000");
			ClientResponse response = service.path("type/addType")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, type);
			ResultCommonVo result=response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/**@Test**/
	public void testfindTypeById() {
		String id = "402881e64458c50c014458c5a56d0001";
		ClientResponse response = service.path("type/getType")
				.queryParam("typeId", id).get(ClientResponse.class);
		ResultTypeVo result = response.getEntity(ResultTypeVo.class);
		Assert.assertNotNull(result.getType());
	}

	/**@Test**/
	public void testUpdateType() {
		Type t = new Type();
		t.setId("402881e6441246a101441248aa0f0001");
		t.setName("修改成功");
		t.setCode("C00000001");
		t.setParentCode("C00000002");
		ClientResponse response = service.path("type/updateType")
				.type(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class, t);
		Assert.assertEquals(Constant.OK, response.getEntity(ResultCommonVo.class).getMessage());

	}

	/**@Test**/
	public void testDeleteType() {
		ClientResponse response = service.path("type/deleteType")
				.queryParam("typeId", "402881e6441246a101441248aa0f0001")
				.delete(ClientResponse.class);
		Assert.assertEquals(Constant.OK, response.getEntity(ResultCommonVo.class).getMessage());

	}

	/**@Test**/
	public void testGetTypeByCritera() {
		ClientResponse response = service.path("type/getTypes")
				.queryParam("pageNo", "1").queryParam("pageSize", "10").queryParam("codeName", "C000001").type(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
		ResultTypeVo result=response.getEntity(ResultTypeVo.class);
		Assert.assertEquals(1, result.getTotalCount());
	}

}
